package com.noocodecommit.wicket.jquery.plugin.ui.resizable;

import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.model.Model;

import com.noocodecommit.wicket.jquery.TemplatePage;
import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.plugin.chili.JQChili;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.resizable.JQResizable;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.resizable.JQResizableUtils;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.resizable.OnResizeStartBehavior;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.resizable.OnResizeStopBehavior;
import com.noocodecommit.wicket.stuff.jquery.plugin.ui.tabs.JQTabs;

public class ResizablePage extends TemplatePage
{

	private static final long serialVersionUID = 1L;
	private final JQTabs tabContainer;

	public ResizablePage()
	{
		super();
		JQCore.init(this);
		this.setupChili();

		tabContainer = new JQTabs("tab-container");
		tabContainer.setOutputMarkupId(true);
		add(tabContainer);

		/*************************
		 * Simple Resizable
		 *************************/
		JQResizable resize0 = JQResizableUtils.create("resize0").autohide(true).minHeight(80).minWidth(150).build();
		tabContainer.add(resize0);

		/*************************
		 * Simple Resizable - All directions
		 *************************/
		JQResizable resize1 = JQResizableUtils.create("resize1").handles("all").minHeight(80).minWidth(150).build();
		tabContainer.add(resize1);

		/*************************
		 * Ajaxed Resizable
		 *************************/
		final JQResizable resize2 = JQResizableUtils.create("resize2").minHeight(80).minWidth(150).build();
		tabContainer.add(resize2);

		final Label output = new Label("output");
		output.setOutputMarkupId(true);
		tabContainer.add(output);

		final Button button2Enable = new Button("button2-enable");
		tabContainer.add(button2Enable.setEnabled(false));

		final Button button2Disable = new Button("button2-disable");
		tabContainer.add(button2Disable.setEnabled(true));

		resize2.add(new OnResizeStartBehavior()
		{
			public void onStart(AjaxRequestTarget target)
			{
				output.setModel(new Model("start resize " + this.getComponent().getMarkupId()));
				target.addComponent(output);
			}
		});

		resize2.add(new OnResizeStopBehavior()
		{
			public void onStop(AjaxRequestTarget target)
			{
				output.setModel(new Model("stop resize " + this.getComponent().getMarkupId()));
				target.addComponent(output);
			}
		});

		button2Enable.add(new AjaxEventBehavior("onclick")
		{
			protected void onEvent(AjaxRequestTarget target)
			{
				resize2.enable(target);
				button2Enable.setEnabled(false);
				button2Disable.setEnabled(true);
				target.addComponent(button2Enable);
				target.addComponent(button2Disable);
			}
		});

		button2Disable.add(new AjaxEventBehavior("onclick")
		{
			protected void onEvent(AjaxRequestTarget target)
			{
				resize2.disable(target);
				button2Enable.setEnabled(true);
				button2Disable.setEnabled(false);
				target.addComponent(button2Enable);
				target.addComponent(button2Disable);
			}
		});

		JQChili chili1 = new JQChili("chili1");
		add(chili1);
		this.bindChiliCode(chili1, this.getClass(), "html");

		JQChili chili2 = new JQChili("chili2");
		add(chili2);
		this.bindChiliCode(chili2, this.getClass(), "java");

		JQCore.get().bindOnDocumentReadyFunction();
	}
}
